package com.shire.web.console.controller.kf;

import com.shire.web.console.common.MD5Util;
import com.shire.web.console.common.Response;
import com.shire.web.console.model.user.SystemUser;
import com.shire.web.console.service.user.SystemUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("/kf")
@Controller
public class KfController {

    @Resource
    private SystemUserService systemUserService;

    @GetMapping("/manage")
    public String manageKf(HttpSession session){
        if (session.getAttribute("user")==null)
            return "redirect:/login";
        return "kf/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Response listKf(HttpSession session){
        Response response = new Response();

        try {
            SystemUser user = (SystemUser) session.getAttribute("user");
            if (user==null){
                response.error("请登录");
                return response;
            }
            response.setData(systemUserService.getKfUsers(user.getId()));
            response.success("操作成功");
        }catch (Exception e){
            response.error("接口错误: "+e.getMessage());
            e.printStackTrace();
        }
        return response;
    }

    @RequestMapping("/resetPwd")
    @ResponseBody
    public Response resetPwd(@RequestBody SystemUser systemUser, HttpSession session){
        Response response = new Response();

        try {
            if (session.getAttribute("user")==null){
                response.error("请登录");
                return response;
            }
            String pwd = MD5Util.MD5Encode("123$%^789");
            systemUser.setPassword(pwd);
            systemUserService.saveUser(systemUser);
            response.success("操作成功");
        }catch (Exception e){
            response.error("接口错误: "+e.getMessage());
            e.printStackTrace();
        }
        return response;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Response updateKf(@RequestBody SystemUser systemUser, HttpSession session){
        Response response = new Response();
        try {
            SystemUser user = (SystemUser) session.getAttribute("user");

            if (user==null){
                response.error("请登录");
                return response;
            }
            if (systemUser.getStatus() == null)
                systemUser.setStatus(0);
            systemUser.setOpUserId(user.getId());
            systemUser.setIsKf(1);
            systemUserService.saveUser(systemUser);
            response.success("操作成功");
        }catch (Exception e){
            response.error("接口错误: "+e.getMessage());
            e.printStackTrace();
        }
        return response;
    }

    @GetMapping("/detail")
    public ModelAndView detail(Integer userId, HttpSession session){
        if (session.getAttribute("user")==null)
            return new ModelAndView("redirect:/login");
        ModelAndView ma = new ModelAndView("kf/detail");
        if (userId == null){
            ma.addObject("user",new SystemUser());
        }else {
            SystemUser user = systemUserService.selectById(userId);
            ma.addObject("user",user);
        }
        return ma;
    }
}
